package com.uy.cuantoconocesdeuruguay.controllers;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import com.google.gson.Gson;
import com.uy.cuantoconocesdeuruguay.MainActivity;
import com.uy.cuantoconocesdeuruguay.datatypes.RandomImageEnvelope;
import com.uy.cuantoconocesdeuruguay.datatypes.UploadImageEnvelope;
import com.uy.cuantoconocesdeuruguay.exceptions.ConnectivityException;
import com.uy.cuantoconocesdeuruguay.server.HttpsExecuter;
import com.uy.cuantoconocesdeuruguay.server.ServerInfo;
import com.uy.cuantoconocesdeuruguay.utiles.ConnectionManager;
import com.uy.cuantoconocesdeuruguay.utiles.NotificationsUtils;
import com.uy.cuantoconocesdeuruguay.utiles.StringPool;

@SuppressLint("NewApi")
public class ImagesController {
	private static final Gson gson = new Gson(); 
	private static final String SERVER_PREFIX = ServerInfo.SERVER_SECURE_HOST;

	public static RandomImageEnvelope getNextImage(String lastImageId) throws ConnectivityException {
		try{    
			String deviceID = MainActivity.userData.getDeviceID();
			String completeURL = SERVER_PREFIX+ServerInfo.NEXT_IMAGE_PATH;
			String query = "deviceid="+deviceID+"&lastImageId="+(lastImageId == null ? StringPool.BLANK : lastImageId);  
			String response = HttpsExecuter.execute(completeURL, query, true);
	        RandomImageEnvelope ret = gson.fromJson(response, RandomImageEnvelope.class);
	        ret.setRelativeImageURL(ret.getImageURL());
	        ret.setImageURL(ServerInfo.SERVER_HOST+ret.getImageURL()); 
	        ret.setThumbnailURL(ServerInfo.SERVER_HOST+ret.getThumbnailURL());
	        return ret; 
		}catch (Exception e){
			GoogleAnalyticsTracker.trackException(MainActivity.instance, e);
       		e.printStackTrace();
       		return null;
		}
	}
	public static void incrementScore(int score, int imageID, boolean isImage) throws ConnectivityException {
		String deviceID = MainActivity.userData.getDeviceID();
		String completeURL = SERVER_PREFIX+ServerInfo.USERS_PATH;
		String query = "deviceid="+deviceID+"&operation=changeScore&score="+score+"&imageID="+imageID+"&isImage="+isImage;
		HttpsExecuter.execute(completeURL, query, false);
	}
	public static void useHint(int imageID, long timeIncremented) throws ConnectivityException  {
		String deviceID = MainActivity.userData.getDeviceID();
		String completeURL = SERVER_PREFIX+ServerInfo.USERS_PATH;
		String query = "deviceid="+deviceID+"&operation=useHint&imageID="+imageID+"&timeIncremented="+timeIncremented;
		HttpsExecuter.execute(completeURL, query, false); 
	}
	public static void useBomb(int imageID, long timeIncremented) throws ConnectivityException  {
		String deviceID = MainActivity.userData.getDeviceID();
		String completeURL = SERVER_PREFIX+ServerInfo.USERS_PATH;
		String query = "deviceid="+deviceID+"&operation=useBomb&imageID="+imageID+"&timeIncremented="+timeIncremented;
		HttpsExecuter.execute(completeURL, query, false); 
	}
	public static void skipImage(int imageID) throws ConnectivityException  {
		String deviceID = MainActivity.userData.getDeviceID();
		String completeURL = SERVER_PREFIX+ServerInfo.USERS_PATH;
		String query = "deviceid="+deviceID+"&operation=skipImage&imageID="+imageID;
		HttpsExecuter.execute(completeURL, query, false);
	}
	public static void submitPicture(final UploadImageEnvelope envelope, final Activity activity) throws ConnectivityException {
		if (! ConnectionManager.isNetworkConnected(activity)){
			throw new ConnectivityException(StringPool.NO_INTERNET_CONNECTION);
		}
		AsyncTask<Void, Void, String> uploader = new AsyncTask<Void, Void, String>(){
			@Override
			protected String doInBackground(Void... params) {
				String deviceID = MainActivity.userData.getDeviceID();	
				String completeURL = SERVER_PREFIX+ServerInfo.UPLOAD_PATH;
				String query = "deviceid="+deviceID+"&operation=submitImage&envelopeJSON="+gson.toJson(envelope);
				return HttpsExecuter.executeUpload(completeURL, query, envelope, true);
			}
			@Override
			protected void onPostExecute(String response) {
				boolean success = response != null;
				NotificationsUtils.createUploadNotification(success, activity);	
			}
		}; 
		if (android.os.Build.VERSION.SDK_INT > 10) {
			try{
				uploader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
			}catch (Exception e){
				GoogleAnalyticsTracker.trackException(activity, e);
				uploader.execute();
			}
		}else{
			uploader.execute();
		}
	}
}
